const router = require('koa-router')();
const user = require('../app/controller/user');
const files = require('../app/controller/files');
const serverUtil = require('../utils/serverUtil');

/**
 * all user list
 */
router.get('/list', async function (ctx, next) {

    try {
        let index = ctx.request.query.index || 0;
        let limit = ctx.request.query.limit || 10;
        let result = await user.getUserList(~~index, ~~limit);
        ctx.body = {
            code: serverUtil.code.SUCCESS,
            data: result
        }
    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "query user list failed");
    }
});

/**
 * add user record
 */
router.post('/add', async function (ctx, next) {

    try {

        let params = ctx.request.body;
        params.role_name = params.role_name || 'ROLE_USER';
        params.image = '/default_head.jpg';
        params.privilege = params.privilege || 0;
        params.description = params.description || '';

        if (!params.name || !params.password) {
            throw new serverUtil.ServerError(serverUtil.code.ILLEGAL_PARAMS);
        }

        let exists = await user.findUser(params.name);
        if(exists){
            throw new serverUtil.ServerError(serverUtil.code.EXISTS);
        }

        let admin_id = await user.addUser(params);

        if (admin_id > 0) {
            params.password = '';
            ctx.body = {
                code: serverUtil.code.SUCCESS,
                data: params
            }
        } else {
            throw new serverUtil.ServerError(serverUtil.code.FAILED);
        }

    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "add user record failed");
    }
});

/**
 * delete user
 */
router.post('/del', async function (ctx, next) {

    try {

        let admin_name = ctx.request.body.name;

        if (!admin_name) {
            throw new serverUtil.ServerError(serverUtil.code.ILLEGAL_PARAMS);
        }

        let db_user = await user.findUser(admin_name);

        if (!db_user) {
            throw new serverUtil.ServerError(serverUtil.code.VERIFY_FAILED);
        }

        let del_count = await user.deleteUser(admin_name, db_user.id);

        if (del_count > 0) {
            ctx.body = {
                code: serverUtil.code.SUCCESS,
                data: null
            }
        } else {
            throw new serverUtil.ServerError(serverUtil.code.FAILED);
        }

    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "delete user record failed");
    }
});

/**
 * update user
 */
router.post('/update', async function (ctx, next) {

    try {

        let params = ctx.request.body;

        if (!params.name) {
            throw new serverUtil.ServerError(serverUtil.code.ILLEGAL_PARAMS);
        }

        let db_user = await user.findUser(params.name);

        if (!db_user) {
            throw new serverUtil.ServerError(serverUtil.code.VERIFY_FAILED);
        }

        params.role_name = params.role_name || db_user.role_name;
        params.privilege = params.privilege || db_user.privilege;
        params.description = params.description || db_user.description;

        let update_count = await user.updateUser(params);


        if (update_count > 0) {
            ctx.body = {
                code: serverUtil.code.SUCCESS,
                data: params
            }
        } else {
            throw new serverUtil.ServerError(serverUtil.code.FAILED);
        }

    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "update user record failed");
    }
});

/**
 * change password
 */
router.post('/password', async function (ctx, next) {

    try {

        let old_pwd = ctx.request.body.old_pwd;
        let new_pwd = ctx.request.body.new_pwd;

        if (!old_pwd || !new_pwd) {
            throw new serverUtil.ServerError(serverUtil.code.ILLEGAL_PARAMS);
        }

        if(!ctx.session || !ctx.session.loginUser){
            throw new serverUtil.ServerError(serverUtil.code.VERIFY_FAILED);
        }

        let admin_name = ctx.session.loginUser.name;

        let db_user = await user.loadUser(admin_name, old_pwd);

        if (!db_user) {
            throw new serverUtil.ServerError(serverUtil.code.VERIFY_FAILED);
        }


        let update_count = await user.changePassword(admin_name, new_pwd);


        if (update_count > 0) {
            ctx.body = {
                code: serverUtil.code.SUCCESS,
                data: null
            }
        } else {
            throw new serverUtil.ServerError(serverUtil.code.FAILED);
        }

    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "change password failed");
    }
});

/**
 * change user avatar
 */
router.post('/avatar', async function (ctx, next) {

    try {

        if(!ctx.session || !ctx.session.loginUser){
            throw new serverUtil.ServerError(serverUtil.code.VERIFY_FAILED);
        }

        let admin_name = ctx.session.loginUser.name;
        let admin_id = ctx.session.loginUser.id;

        let tmp_file = ctx.request.body.files.file;
        let db_file = await files.upload(admin_id, tmp_file);

        let update_count = await user.changeAvatar(admin_name, db_file[0].path);

        if (update_count > 0) {
            ctx.session.loginUser.image = db_file[0].path;
            ctx.body = {
                code: serverUtil.code.SUCCESS,
                data: db_file[0]
            }
        } else {
            throw new serverUtil.ServerError(serverUtil.code.FAILED);
        }

    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }
        serverUtil.logError(ctx, e, "update user avatar failed");
    }
});


module.exports = router;